/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import common.Point;
import common.Settings;
import common.UserRPCClient;
import javax.swing.table.AbstractTableModel;
import service.bboard.BulletinBoard;

/**
 *
 * @author Danny
 */
public class BulletinBoardTabelModel extends AbstractTableModel {

    UserRPCClient bulletinBoardWebService = null;

    public BulletinBoardTabelModel() {
        setBulletinBoardService();
    }

    private void setBulletinBoardService() {
        bulletinBoardWebService = new UserRPCClient(Settings.BBOARD_URI);
    }
    BulletinBoard bb = new BulletinBoard();
    Integer[] users;
    Point[][] shares;

    public void refresh() {
        Class[] returnTypes = new Class[]{Integer[].class};
        String namespaceURI = Settings.BBOARD_NS;
        String methodName = "getListOfClients";
        Object[] mArgs = new Object[]{null};
        users = (Integer[]) this.bulletinBoardWebService.wsCall(returnTypes, namespaceURI, methodName, mArgs)[0];

        System.out.println(users.length);
        shares = new Point[users.length][];
        for (int i = 0; i < users.length; i++) {
            returnTypes = new Class[]{Point[].class};
            methodName = "getPublicShareList";
            mArgs = new Object[]{users[i]};
            shares[i] = (Point[]) this.bulletinBoardWebService.wsCall(returnTypes, namespaceURI, methodName, mArgs)[0];
            System.out.println(i + ": " + shares[i]);
        }
    }

    public int getRowCount() {
        refresh();
        return users.length;
    }

    public int getColumnCount() {
        // refresh();
        return 2 + Settings.threshold;
    }

    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "User pseudonym";
            case Settings.threshold + 1:
                return "ID (secret)";
            default:
                return "Share# " + (columnIndex - 1);
        }
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        refresh();
        System.out.println(rowIndex + " " + columnIndex);
        switch (columnIndex) {
            case 0:
                return users[rowIndex];
            case Settings.threshold + 1:
                Class[] returnTypes = new Class[]{Integer.class};
                String namespaceURI = Settings.BBOARD_NS;
                String methodName = "getUserSecret";
                Object[] mArgs = new Object[]{users[rowIndex]};
                return (Integer) this.bulletinBoardWebService.wsCall(returnTypes, namespaceURI, methodName, mArgs)[0];
            default:
                if (shares[rowIndex].length > columnIndex - 1) {
                    return shares[rowIndex][columnIndex - 1];
                } else {
                    return null;
                }
        }
    }
}
